//设置依赖的配置文件
require.config({
	paths : {
		"jquery" : "jquery",
		"share_top" : "share_top",
		"share_bottom" : "share_bottom",
		"cookie" : "cookie"
	}
})
//设置依赖
require(["jquery","share_top","share_bottom","cookie"],function($,share_top,share_bottom,cookiefn){
	//将头部和尾部导入页面中
	$("#top_box").load("share_top.html",function(){
		share_top.fn();
		$(".top_cart .num span").html(cookiefn.getTotal());
	});
	$("#bottom_box").load("share_bottom.html",function(){
		share_bottom.fn();
	});
	//浮动顶部
	$(".float_top_cart em").html(cookiefn.getTotal());
	$(window).scroll(float_top);
	$(document).ready(float_top);
	function float_top(){
		if($(window).scrollTop() >= 222){
			$("#float_top").css("display","block");
		}else{
			$("#float_top").css("display","none");
		}
	}
	//已选条件 展开
	$open_flag = true;
	$(".second_dl span").click(function(){
		if($open_flag){
			$(".second_dl").css("height","auto");
			$(".second_dl span em").removeClass("icon-xia").addClass("icon-shang");
		}else{
			$(".second_dl").css("height","120px");
			$(".second_dl span em").removeClass("icon-shang").addClass("icon-xia");
		}
		$open_flag = !$open_flag;
	});
	//获取json文件
	$.getJSON('../json/list_goods.json',function(response,status,xhr){
		//alert(response.length);
		$arrTotal = [];
		$arrPrice = [];
		$arrPingNum = [];
		$arrSaleNum = [];
		for($i = 0;$i < response.length;$i ++){
			$arrTotal.push($i);
			$arrPrice.push(parseInt(response[$i].price));
			$arrPingNum.push(parseInt(response[$i].num));
			$arrSaleNum.push(parseInt(response[$i].sale_num));
		}
		//创建list页中的中心商品栏
		addListEle($arrTotal,".list_water");
		//按价格评价数销售数排序的数组
		$arrPrice = arrSort($arrPrice);
		$arrPingNum = arrSort($arrPingNum);
		$arrSaleNum = arrSort($arrSaleNum);
		function arrSort(arr){
			var arr1 = [];
			for(let i = 0;i < arr.length;i ++){
				arr1.push(i);
			}
			//alert(arr1);
			for(var i = 1;i < arr.length;i ++){
				for(var j = 0;j < arr.length-i;j++){
					if(arr[j] > arr[j+1]){
						var t = arr[j];
						arr[j] = arr[j + 1];
						arr[j + 1] = t;
						var y = arr1[j];
						arr1[j] = arr1[j + 1];
						arr1[j + 1] = y;
					}
				}
			}
			//alert(arr1);
			return arr1;
		}
		//获取排序的按钮
		$(".list_sort li a").click(function(){
			$(".list_sort li a").css({"background":"none","color":"#333"});
			$(this).css({"background":"#aeaeae","color":"#fff"});
			switch($(this).parent().index()){
				case 0 : addListEle($arrTotal,".list_water");break;
				case 1 : addListEle($arrSaleNum,".list_water");break;
				case 2 : addListEle($arrPrice.reverse(),".list_water");break;
				case 3 : addListEle($arrPingNum,".list_water");break;
			}
		})
		//创建大家在买的内容
		$arr = [];
		while(1){
			$other_index = getRandom(0,39);
			if($arr.indexOf($other_index) == -1){
				$arr.push(getRandom(0,39));
			}
			if($arr.length == 4){
				addListEle($arr,".other_buy_box")
				break;
			}
		}
		
		function addListEle(arr,clazz){
			$(clazz).empty();
			for($i = 0;$i < arr.length;$i ++){
				$(clazz).append('<div data-good-id="' + response[arr[$i]].goods_id + '"><a href="' + response[arr[$i]].links +'"><div class="list_img_box"><img src="../' + response[arr[$i]].paths1 + '" alt="" /><img src="../' + response[arr[$i]].paths2 + '" alt="" /></div><span class="list_message">' + response[arr[$i]].message + '</span><div class="list_sale"><span class="sale_type">' + response[arr[$i]].type + '</span><span class="sale_message">' + response[arr[$i]].sale_message + '</span></div><i><em>￥</em><span class="list_price">' + response[arr[$i]].price + '</span></i><span class="evaluate_num">评价：<span>' + response[arr[$i]].num + '</span>条</span><img class="gocart_img" src="../' + response[arr[$i]].paths1 + '"/></a><button class="add_btn">加入购物车</button></div>');
			}
			$(".list_con .list_sale .sale_type").each(function(){
				if(this.innerHTML == ""){
					$(this).parent().empty();
				}
			});
			$(".list_con div").attr("num",0);
			$(".list_con .list_img_box").hover(function(){
				$(this).find("img:last-child").stop().animate({"left":0},300);
			},function(){
				$(this).find("img:last-child").stop().animate({"left":230},300);
			});
			//加入购物车按钮事件
			$(clazz + " .add_btn").mousedown(function(event){
				if(cookiefn.getNum($(this).parent().attr("data-good-id"),"goodNum")){
					$(this).parent().attr("num",parseInt(cookiefn.getNum($(this).parent().attr("data-good-id"),"goodNum")) + 1);
				}else{
					$(this).parent().attr("num",1);
					//alert(1);
				}
				cartMove(event,$(this));
			});
		}
		
		//随机函数
		function getRandom(min,max){
			return Math.floor(Math.random() * (max - min) + min);
		}
		//右侧浮动购物车详情创建
		function getRightCartImg(){
			$arr = [];
			$(".right_cart ol").empty();
			while(1){
				$other_index = getRandom(0,39);
				if($arr.indexOf($other_index) == -1){
					$arr.push($other_index);
					$(".right_cart ol").append('<li><a href="#"><img src="../' + response[$other_index].paths1 + '"/><p>￥' + response[$other_index].price + '</p></a></li>')
				}
				if($arr.length == 4){
					break;
				}
			}
		}
		$(".right_cart em").click(function(){
			$(".right_cart").css("display","none");
		})
		function cartMove(event,$obj){
			//弹出购物车弹框
			$(".right_cart").css("display","block");
			getRightCartImg();
			//飞入购物车
			var cloneImg = $obj.parent().find(".gocart_img").clone(true).appendTo('body').css({
				"display":"block",
				"left":event.pageX,
				"top":event.pageY - $(window).scrollTop(),
				"z-index":11111,
				"position":"fixed"
				});
			cloneImg.animate({
				"left":$(".right_cart_btn").offset().left + 10,
				"top":$(".right_cart_btn").offset().top + 20 - $(window).scrollTop(),
				"width":10,
				"height":20,
				"opacity":0.5
			},1000,function(){
				setTimeout(function(){
					cloneImg.remove();
				},50)
			});
			//{"src":"../img/list/list_1_1.jpg","message":"53度 茅台","price":"354","num":"12","sale":"立减20"}
			//创建并获取cookie
			var cooSrc = $obj.parent().find(".gocart_img").attr("src");
			var cooMsg = $obj.parent().find(".list_message").html();
			var cooPrice = $obj.parent().find(".list_price").html();
			var cooEva = $obj.parent().find(".evaluate_num span").html();
			//var cooSale = $obj.parent().find(".sale_message").html();
			var cooNum = $obj.parent().attr("num");
			var cooId = $obj.parent().attr("data-good-id");
			var cooKey = $obj.parent().attr("data-good-id");
			//alert(cooKey);
			var cookieText = '{"goodSrc":"' + cooSrc + '","goodMsg":"' + cooMsg + '","goodPrice":"' + cooPrice + '","goodEva":"' + cooEva + '","goodNum":"' + cooNum + '","goodId":"' + cooId + '"}';
			cookiefn.createCookie(cooKey,cookieText,7,"/");
			var total = cookiefn.getTotal();
			$(".float_top_cart em").html(total);
			$(".top_cart .num span").html(total);
			//alert(total);
		}
	})
})